package com.zynn.service.module.canal.service.impl;

import com.zynn.common.core.service.impl.BaseServiceImpl;
import com.zynn.feign.module.es.SyncEventFeignService;
import com.zynn.message.queue.constant.MqQueueNameConstant;
import com.zynn.message.queue.service.SenderService;
import com.zynn.service.module.canal.dao.UserFriendMapper;
import com.zynn.service.module.canal.entity.UserFriend;
import com.zynn.service.module.canal.service.UserFriendService;
import com.zynn.common.core.utils.TypeConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘天元
 * @since 2018-08-03
 */
@Service
@Slf4j
public class UserFriendServiceImpl extends BaseServiceImpl<UserFriendMapper, UserFriend> implements UserFriendService {

    @Autowired
    private SyncEventFeignService syncEventFeignService;

    @Autowired
    private SenderService senderService;

    @Override
    public void handleInsert(Map<String, Object> columnsMap) {
        /**
         * 产品推荐
         */
        senderService.send(MqQueueNameConstant.FIND_FRIEND_USER_FRIEND, columnsMap);
        /**
         * 被邀请用户根据用户详细个人资料确定用户推荐优先级
         */
        senderService.send(MqQueueNameConstant.PROFILE_FIND_FRIEND_USER_FRIEND, columnsMap);
    }

    @JmsListener(destination = MqQueueNameConstant.FIND_FRIEND_USER_FRIEND)
    public void findDegOneFriend(Map<String, Object> columnsMap) {
        log.info("FIND_FRIEND_USER_FRIEND:{}", columnsMap);
        Long inviteUserId = TypeConvertUtil.objectToLong(columnsMap.get("invite_user_id"));
        if(inviteUserId!=null && inviteUserId!=0) {
            log.info("计算{}一度好友", inviteUserId);
            syncEventFeignService.initProcuctFriend(inviteUserId);
        }
        Long acceptUserId = TypeConvertUtil.objectToLong(columnsMap.get("accept_user_id"));
        if(Objects.nonNull(acceptUserId)){
            log.info("计算{}一度好友", acceptUserId);
            syncEventFeignService.initProcuctFriend(acceptUserId);
        }
    }

    @JmsListener(destination = MqQueueNameConstant.PROFILE_FIND_FRIEND_USER_FRIEND)
    public void findProfileFriend(Map<String, Object> columnsMap) {
        log.info("PROFILE_FIND_FRIEND_USER_FRIEND:{}", columnsMap);
        Long inviteUserId = TypeConvertUtil.objectToLong(columnsMap.get("invite_user_id"));
        Long acceptUserId = TypeConvertUtil.objectToLong(columnsMap.get("accept_user_id"));
        if(
                Objects.nonNull(inviteUserId) && inviteUserId > 0
                && Objects.nonNull(acceptUserId) && acceptUserId > 0) {
            syncEventFeignService.initFriendVersionProfile(inviteUserId,acceptUserId);
        }
    }

}
